package ru.gerunn.silent.message;

import ru.gerunn.silent.exception.SilentSendMessageException;
import ru.gerunn.silent.server.Server;
import ru.gerunn.silent.server.Session;

public class MessageQueueThread extends Thread {

	private MessageQueue queue;
	private Server server;

	public MessageQueueThread(MessageQueue queue, Server server) {
		this.queue = queue;
		this.server = server;
	}

	public void run() {
		Message message;
		try {
			while ((message = queue.get()) != null) {
				for (Session session : this.server.getSessions().values()) {
					try {
						message.send(session.getWriter());
					} catch (SilentSendMessageException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
